# -*- coding: utf-8 -*-
# @Time : 2022/2/25 12:33
# @File : views.py
from django.middleware.csrf import get_token
from django.shortcuts import render
from django import http
from django.urls import reverse
from django.utils import timezone

from Book_exaln_system.models import ExaminationForPeople
from Choose_coach_system.models import ChooseCoach, Blacklist


def main_page(req):
    if req.user.is_authenticated:
        return http.HttpResponseRedirect(reverse('index'))
    return render(req, 'index/frist_page.html')


def index(req):
    if req.user.is_authenticated:
        date = timezone.now()
        examination = ExaminationForPeople.objects.filter(student=req.user).all()
        coach_courses = None
        t_out = None
        blacklist = None
        if req.user.identity_c == '1':
            coach_courses = ChooseCoach.objects.filter(teacher=req.user).all()
        else:
            blacklist = Blacklist.objects.filter(student=req.user).all()
            t_out = ChooseCoach.objects.filter(student=req.user, train_time__gt=timezone.now()).first()
        return render(req, 'index/index.html', context={'user': req.user,
                                                        'book_msg': ChooseCoach.objects.filter(
                                                            student=req.user).all().order_by('-train_time'),
                                                        'datenow': date, 'examination': examination,
                                                        'coach_courses': coach_courses, 'csrf_token': get_token(req),
                                                        'blacklist': blacklist, 'not_time_out': t_out})
    else:
        return http.HttpResponseRedirect(reverse('mainPage'))


def onst(req):
    return render(req, 'index/onst.html', context={})
